package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.BusinessStatement;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/business/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    private String prefix="/business/appointment";

    //去list页面
    @RequestMapping("/listPage")
    public String toList(){
        return prefix+"/list";
    }
    //去添加页面
    @RequestMapping("/addPage")
    public String toadd(){
        return prefix+"/add";
    }
    //去编辑页面
    @RequestMapping("/editPage")
    public String toedit(Long id, Model model){
        Appointment appointment = appointmentService.get(id);
        model.addAttribute("appointment",appointment);
        return prefix+"/edit";
    }
    //分页+过滤
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Appointment> list(AppointmentQuery qo){
        TablePageInfo<Appointment> tablePageInfo = appointmentService.query(qo);
        return tablePageInfo;
    }
    //添加
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Appointment appointment){
        appointmentService.save(appointment);
        return new AjaxResult().success("您的预约单已成功预约！");
    }
    //编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Appointment appointment){
        appointmentService.update(appointment);
        return new AjaxResult().success("您的预约单已成功更新！");
    }
    //到店
    @RequestMapping("/arrivaled")
    @ResponseBody
    public AjaxResult arrival(Long id){
        appointmentService.arrivaled(id);
        return new AjaxResult().success("客户已到店！");
    }
     //取消
    @RequestMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long id){
        appointmentService.cancel(id);
        return new AjaxResult().success("客户订单已成功取消！");
    }
    //软删除
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult softDel(Long id){
        appointmentService.updateIsDel(id);
        return new AjaxResult().success("成功删除该客户预约信息！");
    }
    //生成订单
    @RequestMapping("/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(String appointmentId){//注意处理传过来的是json格式字符串数据
        System.out.println(appointmentId);
        System.out.println("生成订单");
        BusinessStatement businessStatement=appointmentService.generaterStatement(Long.valueOf(appointmentId));
        return AjaxResult.success("订单生成成功！",businessStatement.getId());
    }

}
